home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Toolbox / TwoColumn LDEF / TwoColLDEF.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-07  |  3.9 KB  |  137 lines  |  [TEXT/KAHL]

  1. /*
  2.  * Two-column LDEF code resource
  3.  *
  4.  * looks for a comma in the text of each cell and draws
  5.  * the text that follows the comma half-way across the cell
  6.  *
  7.  * so a cell containing the text
  8.  *    +--------------+
  9.  *    | abc,def      |
  10.  *    +--------------+
  11.  * will instead appear as
  12.  *    +--------------+
  13.  *    | abc   def    |
  14.  *    +--------------+
  15.  *      
  16.  * To see this LDEF in action, paste it into the ModalList
  17.  * sample program and recompile the program so that the LNew call uses
  18.  * the definition procedure 128 rather than 0
  19.  *
  20.  *  Greg Robbins 8/93
  21.  *
  22.  *  Based on Steve Falkenburg's SICN LDEF
  23.  */
  24.  
  25. #include <Lists.h>
  26. #include <SysEqu.h>
  27. #include <ToolUtils.h>
  28. #include <String.h>
  29.  
  30. /* constants for spacing */
  31.  
  32. #define kLeftOffset    2
  33. #define kTopOffset    0
  34.  
  35.  
  36. /* main LDEF entry point */
  37.  
  38. pascal void    main(short lMessage,Boolean lSelect,Rect *lRect,Cell lCell,
  39.                 short lDataOffset,short lDataLen,ListHandle lHandle)
  40. {
  41.     FontInfo fontInfo;                        /* font information (ascent/descent/etc)  */
  42.     ListPtr listPtr;                        /* pointer to store dereferenced list     */
  43.     SignedByte hStateList,hStateCells;        /* state variables for HGetState/SetState */
  44.     Ptr cellData;                            /* points to start of cell data for list  */
  45.     short leftDraw,topDraw;                    /* left/top offsets from topleft of cell  */
  46.     short halfwayLeftDraw;                    /* half way across from left of cell      */
  47.     short substringLen;                        /* number of characters before comma      */
  48.     Ptr commaPtr;                            /* substring beginning at comma           */
  49.     Point currPenPt;                        /* current pen location                   */
  50.     Rect fullCellRect;                      /* unclipped size of cell                 */
  51.     
  52.     /* lock and dereference list mgr handles */
  53.     
  54.     hStateList = HGetState((Handle) lHandle);
  55.     HLock((Handle) lHandle);
  56.     listPtr = *lHandle;
  57.     
  58.     hStateCells = HGetState(listPtr->cells);
  59.     HLock(listPtr->cells);
  60.     
  61.     cellData = *(listPtr->cells);
  62.     
  63.     /* get the size of the cells (since lRect may be clipped) */
  64.     
  65.     LRect(&fullCellRect, lCell, lHandle);
  66.     
  67.     switch (lMessage) {
  68.       case lInitMsg:
  69.           /* we don't need any initialization */
  70.           break;
  71.  
  72.       case lDrawMsg:
  73.         EraseRect(lRect);
  74.         
  75.           if (lDataLen > 0) {
  76.           
  77.               /* determine starting point for drawing */
  78.               
  79.               leftDraw = lRect->left + listPtr->indent.h + kLeftOffset;
  80.               topDraw = lRect->top + listPtr->indent.v + kTopOffset;
  81.               
  82.             /* move to the text location */
  83.             GetFontInfo(&fontInfo);
  84.             MoveTo(leftDraw, topDraw + fontInfo.ascent);
  85.             
  86.             /* if there's a comma, only draw the characters up to the comma
  87.                and then move the data offset and length variables to
  88.                point to the rest of the string following that comma */
  89.              
  90.             /* look for a comma, using memchr from the ANSI libraries (yech) */
  91.             commaPtr = memchr(cellData + lDataOffset, ',', lDataLen);
  92.             
  93.             if (commaPtr != nil) {
  94.             
  95.                 /* draw the substring preceding the comma */
  96.                 substringLen = (short) (commaPtr - (cellData + lDataOffset));
  97.                 DrawText(cellData, lDataOffset, substringLen);
  98.                 
  99.                 /* move the offset to skip the first substring and comma */
  100.                 lDataOffset += substringLen + 1;
  101.                 lDataLen -= (substringLen + 1);
  102.                 
  103.                 /* move to half way across the cell unless the pen
  104.                    is already to the right of that point */
  105.                    
  106.                 halfwayLeftDraw = (fullCellRect.right + leftDraw) / 2;
  107.                 GetPen(&currPenPt);
  108.                 if (currPenPt.h < halfwayLeftDraw)
  109.                     MoveTo(halfwayLeftDraw, topDraw+fontInfo.ascent);
  110.             }
  111.             
  112.             /* draw all remaining characters in the cell */
  113.             DrawText(cellData, lDataOffset, lDataLen);
  114.  
  115.           }
  116.  
  117.         if (!lSelect)
  118.               break;
  119.         
  120.       case lHiliteMsg:
  121.           /* clearing the HiliteMode bit forces the next Invert to really
  122.              be a "hilite" mode inversion */
  123.  
  124.           BitClr((Ptr)HiliteMode,pHiliteBit);
  125.           InvertRect(lRect);
  126.           break;
  127.  
  128.       case lCloseMsg:
  129.           break;
  130.     }
  131.     
  132.     /* restore the handles to their original states */
  133.     HSetState(listPtr->cells,hStateCells);
  134.     HSetState((Handle) lHandle,hStateList);
  135. }
  136.  
  137.